﻿using System.Collections.Generic;
using System.Windows.Input;
using MvvmCross.Core.ViewModels;
using TipCalc.core;
using TipCalc.core.Services;

namespace TipCalc.core.ViewModels
{
    public class TipViewModel :MvxViewModel 
 
    {
        private readonly ICalculation _calculation;
        private int _generosity;
        private double _subTotal;
        private double _tip;

       
        //构造函数注入ICalculation
        public TipViewModel(ICalculation calculation)
        {
            _calculation = calculation;
           
        }

        //总消费
        public double SubTotal
        {
            get { return _subTotal; }
            set
            {
                _subTotal = value;
                RaisePropertyChanged(() => SubTotal);
                Recalculate();
            }
        }

        //消费比例（百分比）
        public int Generosity
        {
            get { return _generosity; }
            set
            {
                _generosity = value;
                RaisePropertyChanged(() => Generosity);
                Recalculate();
            }
        }

        //小费
        public double Tip
        {
            get { return _tip; }
            set
            {
                _tip = value;
                RaisePropertyChanged(() => Tip);
            }
        }

        //总费用
       /* private double _total;
        public double Total
        {
            get { return _total; }
            set { _total = value;
                RaisePropertyChanged(() => Total);
                }
        }*/

        //初始化
        public override void Start()
        {
            _subTotal = 100;
            _generosity = 20;
            Recalculate();
            base.Start();
        }

        private void Recalculate()
        {
            Tip = _calculation.TipAmount(SubTotal, Generosity);
           // Total = SubTotal + Tip;
        }

        public ICommand PayCommand
        {
            get
            {
                return new MvxCommand(() => ShowViewModel<PayViewModel>(new Dictionary<string, string>()
                {
                    { "total",(Tip+SubTotal ).ToString ()},
                }));
            }
        }
    }

}
